var SAP={
    pathUrl:function(){
        return $('base').attr('href');
    },
    nameController:function(){
        var nomeController = "/"+$('base').attr('data-controller')+"/";
        return nomeController;
    },
    url:function(){
        return SAP.pathUrl()+SAP.nameController();
    },
    formatMoney:function(number) {
        var number = number.toString(),
            dollars = number.split('.')[0],
            cents = (number.split('.')[1] || '') +'00';
        dollars = dollars.split('').reverse().join('')
            .replace(/(\d{3}(?!$))/g, '$1.')
            .split('').reverse().join('');
        return dollars + ',' + cents.slice(0, 2);
    },
    getValueInput:function(param){
        var inputValue = $(param).val();
        return inputValue;
    },
    calendario:function(param){
        $(param).datetimepicker({
            pickTime: false
        });
    },
    ExpenseGroup:{
        addOptionGroup:function(value){
            var select = $('#sa-select-sExpenseGroup');
            var novoValue = value.toLowerCase();
            select.find('.novoOption').remove();
            var htmlOption = '<option selected="selected" value="'+value+'" class="novoOption" >'+value+'</option>';
            $('#sa-select-sExpenseGroup').find('option').each(function(e){
                var optionExist = $(this).html().toLowerCase();
                if(novoValue == optionExist){
                    $(this).attr('selected',true);
                    htmlOption = '';
                }else{
                    $(this).attr('selected',false);
                }
            });
            select.append(htmlOption);
        }
    },
    modal:{
        open:function(_class){
            if(_class == null){
                alert('Houve um error ao informar o nome da tela de fechamento.');
                return false;
            }else{
                $(_class).modal({
                    keyboard:false,
                    backdrop:'static',
                    show:true
                });
            }
        },
        close:function(_class){
            if(_class == null){
                alert('Houve um error ao informar o nome da tela de fechamento.');
                return false;
            }else{
                $(_class).modal('hide');
            }
        },
        news:function(_class,tipo){
            switch(tipo){
                case 'lg':
                    classTipo = 'bs-example-modal-lg';
                    classSize = 'modal-lg';
                    break;
                default:
                    classTipo = 'bs-example-modal-static';
                    classSize = '';
                    break
            }
            $("#modalNew").addClass(_class+' '+classTipo);
            $("#modalNew").find('.modal-dialog').addClass(classSize);
        },
        body:function(result){
            $("#modalNew").find('.modal-dialog').find('.modal-content').html('').html(result);
        }
    }
}

var valorTotal = 0;
jQuery(document).ready(function(){
    valorTotal = 0;
    jQuery("#sa-link-aDespesa").click(function(){
        var valor = SAP.getValueInput('#sa-input-txtGroupName');
        SAP.ExpenseGroup.addOptionGroup(valor);
    })
    jQuery(".chosen-select").chosen({'width':'100%','white-space':'nowrap'});
});
function somarValor(e){
    var inputChecked = e.checked;
    if(inputChecked){
        valorTotal = parseFloat(valorTotal)+parseFloat(jQuery(e).attr('data-value'));
    }else{
        valorTotal = parseFloat(valorTotal)-parseFloat(jQuery(e).attr('data-value'));
    }
    valorTotal = valorTotal.toPrecision(10);
    valor = SAP.formatMoney(valorTotal);
    jQuery('.sa-box-ContasReceber-valorTotal').html('').html(valor);
}
function modalClose(_param){
    SAP.modal.open('#tela1');
    SAP.modal.close('#'+_param);
}
